package 链表和树.链表.digui;
//Java：两数相加
public class AddTwoNumbers{
    public static void main(String[] args) {
        Solution solution = new AddTwoNumbers().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
//    public class ListNode {
//        int val;
//        ListNode next;
//        ListNode() {}
//        ListNode(int val) { this.val = val; }
//        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
//    }
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
       return addTwo(l1, l2, 0);
    }

    private ListNode addTwo(ListNode l1,ListNode l2, int carry){
        //递归边界
        if(l1 == null && l2 == null && carry ==0){
            return null;
        }

        int num = carry;
        if(l1 != null){
            num += l1.val;
            l1 = l1.next;
        }
        if (l2 != null){
            num += l2.val;
            l2= l2.next;
        }
        //现在把和取模获取本节点值，商作为下一位进位
        return new ListNode(num %10, addTwo(l1, l2, num/10));
    }

}
//leetcode submit region end(Prohibit modification and deletion)

}